package com.ruoyi.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.system.pojo.vms.Region;
import com.ruoyi.system.pojo.vo.RegionVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * Description:
 *
 * @author JourWon
 * @date Created on 2025/5/15
 */
@Mapper
public interface RegionMapper extends BaseMapper<Region> {
    @Select("<script>" +
            "SELECT COUNT(DISTINCT r.id) " +
            "FROM tb_region r " +
            "LEFT JOIN tb_node n ON r.id = n.region_id " +
            "<where>" +
            "   <if test='name != null and name != \"\"'>" +
            "       AND r.name LIKE CONCAT('%', #{name}, '%')" +
            "   </if>" +
            "</where>" +
            "</script>")
    int countRegions(@Param("name") String name);

    @Select("<script>" +
            "SELECT r.*, COUNT(n.id) AS node_count " +
            "FROM tb_region r " +
            "LEFT JOIN tb_node n ON r.id = n.region_id " +
            "<where>" +
            "   <if test='name != null and name != \"\"'>" +
            "       AND r.name LIKE CONCAT('%', #{name}, '%')" +
            "   </if>" +
            "</where>" +
            "GROUP BY r.id " +
            "LIMIT #{offset}, #{pageSize}" +
            "</script>")
    List<RegionVO> searchRegions(@Param("name") String name, @Param("offset") int offset, @Param("pageSize") Integer pageSize);
}
